const express = require('express')
var bodyParser = require('body-parser') 
const app = express()
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())
const port = 3000

const configs = require('./configs')
const mysql = require('mysql')
const connection = mysql.createConnection(configs.mysql)
connection.connect()

app.get('/get',(req,res)=>{
    let sql = `select * from emp1 where age between 20 and 40 ;`
    connection.query(sql,(err,result)=>{
        if(!err){
            // console.log('数据库连接成功')
            res.send(result)
        }else{
            console.log('数据库连接失败')
        }

    })
})
app.post('/add',(req,res)=>{
    let sql = `insert into emp1 ( workno, name, gender, age, idcard, workaddress, entrydate) values ('${req.body.workno}','${req.body.name}','${req.body.gender}','${req.body.age}','${req.body.idcard}','${req.body.workaddress}','${req.body.entrydate}')`
    connection.query(sql,(err,result)=>{
        if(!err){
            res.send("添加成功！")
        }else{
            console.log('数据库连接失败')
        }

    })
})
app.post('/edit',(req,res)=>{
    console.log(req)
    let sql = `update emp1 set name='${req.body.name}',gender='${req.body.gender}',age='${req.body.age}',idcard='${req.body.idcard}',workaddress='${req.body.workaddress}',entrydate='${req.body.entrydate}' where workno=${req.body.workno}`
    connection.query(sql,(err,result)=>{
        if(!err){
            res.send("修改成功！")
        }else{
            console.log('数据库连接失败')
        }

    })
})
app.get('/delete',(req,res)=>{
    let sql = `delete from emp1 where workno=${req.query.workno} ;`
    connection.query(sql,(err,result)=>{
        if(!err){
            res.send("删除成功！")
        }else{
            console.log('数据库连接失败')
        }

    })
})

app.listen(port,()=>{
    console.log('服务启动成功！！！')
})